package com.wangsd.client;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.alibaba.fastjson.JSON;
import com.wangsd.utils.CompareToUtils;
import com.wangsd.utils.DateUtils;
import com.wangsd.utils.MyDecimalUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
public class MyClient {

    public void start() {
        log.debug("--------开始执行---------" + DateUtils.formatDatetime(new Date()));
        List<ExportBean> standardList = readFile("C://excel//标准数据.xlsx");
        for (ExportBean bean : standardList) {
            String str2 = replaceString(bean.getName());
            bean.setSampleName(str2);
        }
        List<ExportBean> matchingList = readFile("C://excel//待匹配数据.xlsx");
        List<ExportBean> list = new ArrayList<>();
        for (ExportBean matching : matchingList) {
            String str1 = replaceString(matching.getMatchingName());
            if (StringUtils.isBlank(str1)) {
                continue;
            }
            List<ExportBean> tempList = new ArrayList();
            for (ExportBean standard : standardList) {
                String str2 = standard.getSampleName();
                if (StringUtils.isBlank(str2)) {
                    continue;
                }
                double similarity = CompareToUtils.compareTo(str1, str2);
                if (similarity > 0.8) {
                    ExportBean cellList = new ExportBean();
                    cellList.setMatchingName(matching.getMatchingName());
                    cellList.setNum(standard.getNum());
                    cellList.setName(standard.getName());
                    cellList.setSimilarity(MyDecimalUtils.format(MyDecimalUtils.mul(similarity, 100)) + "%");
                    //把大于0.8的单元格放到tempList里面
                    tempList.add(cellList);
                }
            }
            if (tempList.size() > 0) {
                list.addAll(tempList);
            }
        }
        this.writeFile("C://excel//导出内容.xlsx", list);
        log.debug("--------执行完毕---------" + DateUtils.formatDatetime(new Date()));

    }

    private static List<ExportBean> readFile(String str) {
        List<ExportBean> personList = null;
        try {
            FileInputStream file = new FileInputStream(str);
            ImportParams importParams = new ImportParams();
            importParams.setTitleRows(0);
            importParams.setHeadRows(1);
//            importParams.setKeyIndex(null);
            personList = ExcelImportUtil.importExcel(file, ExportBean.class, importParams);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return personList;
    }

    private static void writeFile(String str, List<ExportBean> list) {
        try {
            ExportParams exportParams = new ExportParams();
            exportParams.setType(ExcelType.XSSF);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, ExportBean.class, list);
            FileOutputStream fos = new FileOutputStream(str);
            workbook.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String replaceString(String str) {
        if (StringUtils.isNotBlank(str)) {
            str = str.replaceAll("股份有限公司", "");
            str = str.replaceAll("有限责任公司", "");
            str = str.replaceAll("有限公司", "");
            str = str.replaceAll("连锁", "");
            str = str.replaceAll("医院", "");
        }
        return str;
    }

}
